import axios, { AxiosRequestConfig } from "axios";
//网络封装请求
import { ElMessage } from 'element-plus'
import router from "../../router";
export  function request(config: AxiosRequestConfig<any>) {
    const instance =axios.create({
        baseURL:'',
        timeout:50*1000
    });
    //请求拦截
    instance.interceptors.request.use(config=>{
        const token =window.localStorage.getItem('token');
        if(token&&config.headers){
            config.headers.Authorization = 'Bearer '+token;
        }
        return config
    },err=>{

    });
    //响应拦截
    instance.interceptors.response.use(res=>{
        return res.data?res.data:res;
    },err=>{
        //如果有需要授权，才可以访问的接口，同意去login授权
        if (err.response.status=='401'){
            ElMessage.error('您尚未登录！');
            router.push({path:'/Login'})
        }
        //如果有错误，在这里面会处理，显示错误信息
      
    });
    return instance(config);
}